package com.flute.framework.data.mysql;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import com.flute.framework.data.constants.Constants;
import com.flute.tools.data.mysql.ConnectionInformation;
import com.flute.tools.data.mysql.NewMysqlHandler;
import com.mysql.jdbc.Connection;

public class MySQLConnectionUtil {
	private static final String DATABASE_PROPERTIES = "database.properties";
	private static ConnectionInformation connectionInfo = null;
	private static NewMysqlHandler handler = null;
	private static String songTableName;
	private static String singerTableName;
	private static String albumTableName;
	private static String checkSumPath;
	private static String checkSumFailedPath;
	private static String checkSumBackupPath;

	public synchronized static String getCheckSumBackupPath() {
		if (checkSumBackupPath == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				checkSumBackupPath = prop
						.getProperty(Constants.CHECK_SUM_BACKUP_PATH);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return checkSumBackupPath;
	}

	public synchronized static String getCheckSumFailedPath() {
		if (checkSumFailedPath == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				checkSumFailedPath = prop
						.getProperty(Constants.CHECK_SUM_FAILED_PATH);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return checkSumFailedPath;
	}

	public static String getIndexTable(String tableName) {
		return tableName + "_" + Constants.INDEX;
	}

	public static String getAllRecordsTable(String tableName) {
		return tableName + "_" + Constants.HISTORY_RECORDS;
	}

	public static String getSnapshotTable(String tableName) {
		return tableName + "_" + Constants.SNAPSHOT;
	}

	public static String getHistoryTable(String tableName) {
		return tableName + "_" + Constants.HISTORY;
	}

	public synchronized static ConnectionInformation getConnectionInfo() {
		if (connectionInfo == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				connectionInfo = new ConnectionInformation();

				connectionInfo.setDbName(prop.getProperty("dbName"));
				connectionInfo.setHost(prop.getProperty("dbIp"));
				connectionInfo.setPort(prop.getProperty("dbPort"));
				connectionInfo.setUserName(prop.getProperty("dbUsername"));
				connectionInfo.setPassword(prop.getProperty("dbPassword"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return connectionInfo;
	}

	public static void main(String[] args) {
		getNewMysqlHandler();
		getNewMysqlHandler();
	}

	public synchronized static NewMysqlHandler getNewMysqlHandler() {
		if (handler == null || handler.getConnection() == null) {
			handler = new NewMysqlHandler(getConnectionInfo());
			try {
				handler.getConnection().setTransactionIsolation(
						Connection.TRANSACTION_READ_COMMITTED);
			} catch (SQLException e) {
				throw new RuntimeException("设置事务隔离级别失败");
			}
		}

		return handler;
	}

	public synchronized static String getCheckSumPath() {
		if (checkSumPath == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				checkSumPath = prop.getProperty(Constants.CHECK_SUM_PATH);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return checkSumPath;
	}

	public synchronized static String getSongTableName() {
		if (songTableName == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				songTableName = prop.getProperty(Constants.SONG_TABLE_NAME);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return songTableName;
	}

	public synchronized static String getAlbumTableName() {
		if (albumTableName == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				albumTableName = prop.getProperty(Constants.ALBUM_TABLE_NAME);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return albumTableName;
	}

	public synchronized static String getSingerTableName() {
		if (singerTableName == null) {
			try {
				Properties prop = new Properties();
				prop.load(MySQLConnectionUtil.class.getClassLoader()
						.getResourceAsStream(DATABASE_PROPERTIES));
				singerTableName = prop.getProperty(Constants.SINGER_TABLE_NAME);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return singerTableName;
	}
}
